﻿using JsonArchive;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HousekeepingSystem
{
    public partial class Form1_1 : Form
    {
        public Form1_1()
        {
            InitializeComponent();
        }

        private void Form1_1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd");
            textBox4.Text = DateTime.Now.AddYears(1).AddDays(-1).ToString("yyyy-MM-dd");
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            try
            {
                dt = Convert.ToDateTime(textBox3.Text);
            }
            catch
            {
                textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd");
                dt = DateTime.Now;
            }
            textBox3.Text = dt.ToString("yyyy-MM-dd");
            textBox4.Text = dt.AddYears(1).AddDays(-1).ToString("yyyy-MM-dd");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkContractTable()) return;
            saveContractTable();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        //验证数据
        private bool checkContractTable()
        {
            if (textBox1.Text.Trim() == "")
            {
                textBox1.Focus();
                MessageBox.Show("项目地址不能为空","错误");
                return true;
            }
            if (textBox2.Text.Trim() == "")
            {
                textBox2.Focus();
                MessageBox.Show("业主房号不能为空", "错误");
                return true;
            }
            try
            {
                Convert.ToDateTime(textBox3.Text);
            }
            catch
            {
                textBox3.Focus();
                MessageBox.Show("签约日期格式错误", "错误");
                return true;
            }
            try
            {
                Convert.ToDateTime(textBox4.Text);
            }
            catch
            {
                textBox4.Focus();
                MessageBox.Show("合同期限格式错误", "错误");
                return true;
            }
            try
            {
                Convert.ToDecimal(textBox5.Text);
                if (textBox5.Text.Length < 6 || textBox5.Text.Length > 12)
                {
                    textBox5.Focus();
                    MessageBox.Show("业主电话长度错误", "错误");
                    return true;
                }
            }
            catch
            {
                textBox5.Focus();
                MessageBox.Show("业主电话格式错误", "错误");
                return true;
            }
            try
            {
                Convert.ToDecimal(textBox6.Text);
            }
            catch
            {
                textBox6.Focus();
                MessageBox.Show("合同金额格式错误", "错误");
                return true;
            }
            try
            {
                Convert.ToDecimal(textBox7.Text);
            }
            catch
            {
                textBox7.Focus();
                MessageBox.Show("单次收费格式错误", "错误");
                return true;
            }
            try
            {
                Convert.ToDecimal(textBox8.Text);
            }
            catch
            {
                textBox8.Focus();
                MessageBox.Show("合同编号格式错误", "错误");
                return true;
            }
            try
            {
                Convert.ToDecimal(textBox9.Text);
            }
            catch
            {
                textBox9.Focus();
                MessageBox.Show("发票编号格式错误", "错误");
                return true;
            }

            return false;
        }

        //保存数据
        private void saveContractTable()
        {
            String JSON = File.ReadAllText(Application.StartupPath + @"\info\ContractTable.json");
            var ContractTable = J2D.JsonToDictionary(JSON);            
            ArrayList ContractRowValue = new ArrayList();
            ContractRowValue.Add(comboBox1.SelectedItem.ToString());
            ContractRowValue.Add(textBox1.Text);
            ContractRowValue.Add(textBox2.Text);
            ContractRowValue.Add(textBox3.Text);
            ContractRowValue.Add(textBox4.Text);
            ContractRowValue.Add(textBox5.Text);
            ContractRowValue.Add(textBox6.Text);
            ContractRowValue.Add(textBox7.Text);
            ContractRowValue.Add((Convert.ToDecimal(textBox6.Text) / Convert.ToDecimal(textBox7.Text)).ToString());
            ContractRowValue.Add(textBox6.Text);
            ContractRowValue.Add((Convert.ToDecimal(textBox6.Text) / Convert.ToDecimal(textBox7.Text)).ToString());
            ContractRowValue.Add(textBox8.Text);
            ContractRowValue.Add(textBox9.Text);
            int lastTable = Convert.ToInt32(ContractTable.Keys.Last());
            ContractTable.Add((lastTable + 1).ToString(), ContractRowValue.ToArray());
            JSON = D2J.DictionaryToJson(ContractTable);
            File.WriteAllText(Application.StartupPath + @"\info\ContractTable.json", JSON);
        }

        private void Form1_1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.button1_Click(sender, e);
                e.Handled = true;
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.button2_Click(sender, e);
                e.Handled = true;
            }
        }
    }
}
